package com.sgb.kjwl.viewmodel.self;


import com.sgb.kjwl.api.APIManager;
import com.sgb.link.entity.AreaEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.swgk.core.util.UiUtil;
import com.sgb.kjwl.view.ui.activity.self.SuggestionFeedbackActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.databinding.ObservableField;


import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class SuggestionFeedbackModel extends BaseViewModel {
    private SuggestionFeedbackActivity mActivity;
    private SelfRepertory repertory;

    public ObservableField<UserMsgEntity> userInfo = new ObservableField<>();
    private List<BaseAreaEntity> sexList = new ArrayList<>();

    //PersonInfoModel有参方法
    public SuggestionFeedbackModel(SuggestionFeedbackActivity activity, SelfRepertory repertory) {
        this.mActivity = activity;
        this.repertory = repertory;
    }


    public void feedback(Map map) {
        APIManager.getInstance().getUserAPI().feedback(map).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mActivity.feedback();
                } else {
                    MToast.showToast(mActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mActivity, t.getMessage());
            }
        });
    }


    public void plateSelect() {
        DialogHelper.showProgressDialog(mActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().plateSelect().enqueue(new Callback<BaseEntity<List<AreaEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AreaEntity>>> call, Response<BaseEntity<List<AreaEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    UiUtil.showToast("服务连接超时");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mActivity.plateSelect(response.body().getData());
                } else {
                    MToast.showToast(mActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AreaEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mActivity, t.getMessage());
            }
        });
    }


    //上传多图片
    public void uploadMutiFileModel(File file, int type, int completeNum) {
        if (file.length() > (2 * 1024 * 1024)) {
            DialogHelper.dismissProgressDialog();
            UiUtil.showToast("图片大于2M，上传失败");
            return;
        }
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    UiUtil.showToast("服务连接超时");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mActivity.uploadRefresh(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mActivity, t.getMessage());
            }
        });
    }
}
